<template>
  <el-container class="home-container">
    <el-aside style="width: 200px">
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="header-container">
          <img src="../assets/LoGo.png" class="iconImg" alt=""/>
        </div>
        <!-- 给侧边栏加上按钮实现菜单栏的展开/收起 -->
        <!--        <div class="toggle-button" @click="toggleCollapse">|||</div>-->
        <!-- 侧边菜单栏 -->
        <el-menu background-color="#000000" text-color="#ffffff" active-text-color="#409eff" unique-opened
                 :collapse="isCollapse" :collapse-transition="false" router :default-active="$route.path">
          <!-- 一级菜单 :default-active="activePath" -->
          <!-- 每一个el-submenu都有一个index属性，如果index属性相同，那么就会导致同名的标签在点击时同时展开同时收起 -->
          <!-- 这里的index被规定了只接收字符串，而id是数，所以做了下拼接 -->
          <el-submenu :index="first.id + ''" v-for="first in menuList" :key="first.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <!--              <i :class="first.icon"></i>-->
              <!--              <font-awesome-icon :icon="{{first.icon}}" />-->

<!--              </i>-->
              <span>{{ first.title }}</span>
            </template>
            <!-- 二级菜单 -->
            <!-- 为ele-menu开启router路由模式后，规定：会在激活导航时以 index 作为 path 进行路由跳转。所以把index值设置为路径，也不影响index是唯一标识的作用-->
            <el-menu-item v-if="!first.children?.length" :index="first.url" :key="first.id" @click="jumpTo(first)">
              <template slot="title">
                <!-- 图标 -->
                <!--                <i :class="first.icon"></i>-->
                <!-- 文本 -->
                <span>{{ first.title }}</span>
              </template>
            </el-menu-item>
            <template v-for="second in first.children">
              <el-menu-item v-if="!second.children?.length" :index="second.url" :key="second.id"
                            @click="jumpTo(second)">
                <template slot="title">
                  <!-- 图标 -->
                  <i :class="second.icon"></i>
                  <!--                  <font-awesome-icon :icon="second.icon" />-->
                  <!-- 文本 -->
                  <span>{{ second.title }}</span>
                </template>
              </el-menu-item>
            </template>
          </el-submenu>
        </el-menu>
      </el-aside>
    </el-aside>
    <el-container class="inner-container">
      <!--      头部-->
      <el-header class="inner-header">
        <el-breadcrumb separator="/" class="inner-breadcrumb"  >
          <el-breadcrumb-item v-for="item in tabList" :key="item.path"  :to="{ path: item.path }"
                              class="myColor" :class=" {'is-active': $route.path === item.path}">
            {{ item.title }}
          </el-breadcrumb-item>
        </el-breadcrumb>
        <div class="buttonloginOut">
          <el-button type="primary" class="buttonloginOut" size="mini" @click="logout">退出</el-button>
        </div>
      </el-header>
      <!--      主体-->
      <el-main class="mainDiv">
        <router-view></router-view>
      </el-main>
    </el-container>

  </el-container>
</template>

<script>
  import {mapState, mapMutations, mapActions} from 'vuex'
  import {authLogout} from '@/api/api.js'

  export default {
    name: 'home',

    computed: {
      ...mapState('m_tab', ['tabList']),
      ...mapState('m_user', ['user'])
    },
    data() {
      return {
        listTab: [],
        redirectUrl: 'https://accounts.saic-hongyan.com/login?service=http://localhost:3001/',
        // 将获取到的菜单数据存到data中以便在页面上渲染出来——左侧菜单数据
        // 服务器返回的数组是一个二维数组，也就对应着一级菜单和二级菜单，二级菜单在一级菜单的children中
        // 两层的菜单渲染用双层for循环即可，二级菜单内容的获取----遍历一级菜单的children
        menuList: [],
        // 控制折叠或展开的flag
        isCollapse: false,
        // 被激活的链接地址
        activePath: '',
      }
    },
    created() {
      this.menuList = this.user.menus
    },
    methods: {
      changeMenu(item) {
        this.$router.push({name: item.name})
      },
      ...mapMutations('m_tab', ['SelectMenu', 'deleteMenu']),
      logout() {
        // 退出两步：重定向到,需要token
        const token = localStorage.getItem('token')
        authLogout(token).then(response => {
          if (response.code === 0) {
            this.$message({
              message: '退出登录',
              type: 'success',
            })
            window.location.href = this.redirectUrl
            store.commit('m_user/removeToken')
            store.commit('m_user/removeTicket')
            localStorage.clear()
            return;
          } else {
            return
          }
        });
      },

      // 点击按钮切换菜单的折叠与展开
      toggleCollapse() {
        this.isCollapse = !this.isCollapse
      },

      jumpTo(item) {
        // 防止自己跳自己的报错
        if (this.$route.path !== item.path && !(this.$route.path === '/home' && (item.path === '/'))) {
          this.$router.push(item.path)
        }
        this.listTab = this.tabList
        // 面包屑
        this.SelectMenu(item)
      },
    },
  }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100vh;
    display: flex;
    flex-direction: row;
  }

  .iconImg {
    width: 100%;
    height: 100%;
  }

  img {
    width: 75px;
  }

  .el-submenu__title i {
    color: #fff;
  }

  .el-menu-item i {
    color: #fff;
  }

  .inner-header {
    color: #409EFF !important;
  }

    .myColor /deep/ .el-breadcrumb__inner {
      color: #fff ;
    }
  .myColor /deep/ .el-breadcrumb__inner :hover{
    color: #409EFF !important;
  }
  .is-active /deep/ .el-breadcrumb__inner{
    color: #409EFF; /* 或其他你希望的高亮颜色 */
  }
  /* 鼠标悬停 */
  .el-breadcrumb /deep/ .el-breadcrumb__item:hover .el-breadcrumb__inner {
    color: #409EFF !important;
    font-weight: 400 !important;
  }

  .inner-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
  }

  /* 使其高度占满整个外层容器高度   // 头部采用flex布局*/
  .el-header {
    background-color: #000000;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    /*/ / 让按钮上下居中（垂直居中）而不是上下贴边*/
    align-items: center;
    font-size: 24px;
  }

  .inner-breadcrumb {
    font-size: 15px;
    padding: 30px 10px 20px 15px;

  }
  .el-aside {
    flex: 0 0 auto;
    /* 不参与高度调整，但宽度可变 */
    height: 100%;
    /* 设置 aside 高度为父容器高度 */
    background-color: #000000;
    color: white;
  }

  .header-container {
    display: flex;
    align-items: center;
    height: 60px;
    background-color: dodgerblue;
  }



  .el-menu {
    border-right: 0;
    /*border-right: null;*/
    background-color: #000000;
    color: white;
  }

  .el-main {
    padding: 0px;
  }


  .inner-tag {
    margin-right: 10px;
  }

  .toggle-button {
    background-color: rgb(210, 241, 243);
    font-size: 10px;
    line-height: 24px;
    color: rgb(168, 149, 162);
    text-align: center;
    cursor: pointer;
    /*/ / 让字符之间有一定的0 .2 em的距离 letter-spacing: 0.2 em;*/
  }
</style>
